﻿using ScanConfirm.Business;
using ScanConfirm.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScanConfirmForm
{
    public partial class FrmPartSet : Form
    {
        PartsSetupModel _PartsSetupModel;
        PartsSetupBLL _PartsSetupBLL = new PartsSetupBLL();
        public Action RefreshUiEvent;
        public FrmPartSet()
        {
            InitializeComponent();
        }

        public FrmPartSet(PartsSetupModel partsSetupModel)
        {
            InitializeComponent();
            _PartsSetupModel = partsSetupModel;
            txtCarCode.Text = partsSetupModel.CarCode;
            txtPartsCode.Text = partsSetupModel.PartsCode;
            txtMark.Text = partsSetupModel.Mark;
            txtDisplayCode.Text = partsSetupModel.DisplayCode;
            txtCode.Text = partsSetupModel.Code;
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_PartsSetupModel == null)
            {
                var model = _PartsSetupBLL.GetPartsSetupByCode(txtCode.Text);
                if(model != null)
                {
                    MessageBox.Show("该程序号已配置，不能重复！");
                    return;
                }
                _PartsSetupModel = new PartsSetupModel();
                _PartsSetupModel.CarCode = txtCarCode.Text;
                _PartsSetupModel.PartsCode = txtPartsCode.Text;
                _PartsSetupModel.Mark = txtMark.Text;
                _PartsSetupModel.DisplayCode = txtDisplayCode.Text;
                _PartsSetupModel.Code = txtCode.Text;
                _PartsSetupModel.SyncFlag = 0;

                var result = _PartsSetupBLL.Add(_PartsSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加部品配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加部品配置失败：" + result.Dsca);
            }
            else
            {
                _PartsSetupModel.CarCode = txtCarCode.Text;
                _PartsSetupModel.PartsCode = txtPartsCode.Text;
                _PartsSetupModel.Mark = txtMark.Text;
                _PartsSetupModel.Code = txtCode.Text;
                _PartsSetupModel.DisplayCode = txtDisplayCode.Text;
                _PartsSetupModel.SyncFlag = 2;
                var result = _PartsSetupBLL.Update(_PartsSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改部品配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改部品配置失败：" + result.Dsca);
            }
        }
    }
}
